﻿<%@ WebHandler Language="C#" Class="ApsZsTeamShiftHandler" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;

public class ApsZsTeamShiftHandler : IHttpHandler, IRequiresSessionState
{

    CurrentUserInfo m_UserInfo;
    ResourceMaintBusiness resourceBusiness = new ResourceMaintBusiness();//设备类
    WorkShiftBusiness workShiftBusiness = new WorkShiftBusiness();//班次类
    uLMSCommonBusiness commonBusiness = new uLMSCommonBusiness();//通用类
    TestRunBillBusiness runBill = new TestRunBillBusiness();
    ApsZsShiftAndTeamBusiness ApsST = new ApsZsShiftAndTeamBusiness();
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "InitPageData": //初始化页面数据
                    InitPageData(context);
                    break;
                case "GetWorkCenter": //获取工区
                    GetWorkCenterList(context);
                    break;
                case "GetTeam"://查询组
                    GetTeam(context);
                    break;
                case"GetShift"://查询班
                    GetShift(context);
                    break;    
                case "GetEmployee"://查询员工
                    GetEmployee(context);
                    break;
                case "GetShiftWorkshiftMap"://班次关联到班----查询
                    GetShiftWorkshiftMap(context);
                    break;
                case "SaveShiftWorkshiftMap"://班次关联到班----保存、更新
                    SaveShiftWorkshiftMap(context);
                    break;
                case "GetApsShiftAndTeamRelation"://获取组和分厂，工区，班，版次关联信息
                    GetApsShiftAndTeamRelation(context);
                    break;
                case "BindTeamAndWorkshiftAndEmployee"://更新组 绑定的班次信息;人员信息
                    BindTeamAndWorkshiftAndEmployee(context);
                    break;
                case "GetTeamEmployees"://查询--班组关联的人员信息
                    GetTeamEmployees(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    /// <summary>
    /// 初始化页面数据
    /// </summary>
    /// <param name="context"></param>
    public void InitPageData(HttpContext context)
    {
        DataSet dtSet = new DataSet();
        //获取班次
        DataTable dtWorkShift = workShiftBusiness.GetWorkShiftList();
        dtWorkShift.TableName = "dtWorkShift";
        DataRow drws = dtWorkShift.NewRow();
        drws["WORKSHIFTID"] = "";
        drws["WORKSHIFTNAME"] = "";
        dtWorkShift.Rows.InsertAt(drws, 0);//插入到第一行
        dtSet.Tables.Add(dtWorkShift);
        //获取班
        DataTable dtShift = commonBusiness.GetShiftByFactoryID(m_UserInfo.FactoryID.ToString());
        dtShift.TableName = "dtShift";
        dtSet.Tables.Add(dtShift);
        
        //获取工厂
        Dictionary<string, string> dic = new Dictionary<string, string>();
        //dic.Add("FactoryGone", "");
        DataTable dt = commonBusiness.GetFactory(dic);
        DataView dv = dt.DefaultView;
        dv.RowFilter = " Factoryid='"+m_UserInfo.FactoryID.ToString()+"'";
        DataTable dtFactory = dv.ToTable();
        dtFactory.TableName = "dtFactory";
        dtSet.Tables.Add(dtFactory);
        //获取日历
        //DataTable dtMakeCalendar = resourceBusiness.GetMakeCalendarInfo();
        //dtMakeCalendar.TableName = "dtMakeCalendar";
        //DataRow drmc = dtMakeCalendar.NewRow();
        //drmc["MAKECALENDARID"] = "";
        //drmc["MAKECALENDARNAME"] = "";
        //dtMakeCalendar.Rows.InsertAt(drmc, 0);//插入到第一行
        //dtSet.Tables.Add(dtMakeCalendar);
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { DBDataSet = dtSet }));
        
        
    }
    /// <summary>
    /// 获取工区（通过工厂）
    /// </summary>
    /// <param name="context"></param>
    public void GetWorkCenterList(HttpContext context)
    {
        string factoryId = context.Request["factoryId"].ToString();
        //获取工区
        DataTable dtWorkCenter = commonBusiness.GetWorkCenterByFacoryID(factoryId, true);
        DataRow drWorkCenter = dtWorkCenter.NewRow();
        drWorkCenter["WORKCENTERID"] = "";
        drWorkCenter["WORKCENTERNAME"] = "全部";
        dtWorkCenter.Rows.InsertAt(drWorkCenter, 0);//插入到第一行
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, DBTable = dtWorkCenter }));
    }
    /// <summary>
    /// 获取组
    /// </summary>
    /// <param name="context"></param>
    public void GetTeam(HttpContext context)
    {
        string workCenterId = context.Request["workCenterId"].ToString();
        string shiftId = context.Request["shiftId"].ToString();
        //if (string.IsNullOrWhiteSpace(workCenterId))
        //{
        //    context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "没有工区" }));
        //    return;
        //}
        DataTable dtTeam = null;
        if (!string.IsNullOrWhiteSpace(workCenterId))
        {
            dtTeam=commonBusiness.GetTeam(workCenterId);
        }
        else {
            dtTeam=commonBusiness.GetTeamByShiftID(shiftId);
        }
        DataRow drteam = dtTeam.NewRow();
        drteam["TEAMID"] = "";
        drteam["TEAMNAME"] = "全部";
        dtTeam.Rows.InsertAt(drteam, 0);//插入到第一行
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "获取组成功", DBTable = dtTeam }));
    }
    /// <summary>
    /// 获取班
    /// </summary>
    public void GetShift(HttpContext context)
    {
        string workCenterId = context.Request["workCenterId"].ToString();
        DataTable dtShift = commonBusiness.GetShiftInfoByWorkcenterID(workCenterId);
        if (dtShift.Rows.Count == 0) { dtShift=commonBusiness.GetShiftByFactoryID(m_UserInfo.FactoryID); }
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "获取班成功", DBTable = dtShift }));
    }
    /// <summary>
    /// 查询分厂下人员
    /// </summary>
    public void GetEmployee(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var para=new Dictionary<string,string>();
        para.Add("FactoryID", m_UserInfo.FactoryID.ToString());
        para.Add("FullName", dt.Rows[0]["FullName"].ToString());
        para.Add("EmployeeName", dt.Rows[0]["EmployeeName"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = runBill.GetEmployees(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 日历关联到班----查询
    /// </summary>
    /// <param name="?"></param>
    protected void GetShiftWorkshiftMap(HttpContext context){
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var para = new Dictionary<string, string>();
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["Factory"].ToString()))
        {
            para.Add("FactoryID", dt.Rows[0]["Factory"].ToString());
        }
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["WorkCenter"].ToString()))
        {
            para.Add("WorkcenterID", dt.Rows[0]["WorkCenter"].ToString());
        }
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = ApsST.GetApsShiftAndWorkshift(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 日历关联到班----保存、更新
    /// </summary>
    /// <param name="context"></param>
    protected void SaveShiftWorkshiftMap(HttpContext context) {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var para = new Dictionary<string, string>();
        para.Add("WorkshiftID", dt.Rows[0]["WorkshiftID"].ToString());
        para.Add("ShiftID", dt.Rows[0]["ShiftID"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.Result = ApsST.BindShiftAndWorkshift(para) > 0;
        result.Message = result.Result ? "保存成功" : "保存失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取组和分厂，工区，班，版次关联信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetApsShiftAndTeamRelation(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var para = new Dictionary<string, string>();
        para.Add("FactoryID", string.IsNullOrWhiteSpace(dt.Rows[0]["Factory"].ToString()) ? m_UserInfo.FactoryID.ToString() : dt.Rows[0]["Factory"].ToString());
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["WorkCenter"].ToString()))
        {
            para.Add("WorkcenterID", dt.Rows[0]["WorkCenter"].ToString());
        }
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["Team"].ToString()))
        {
            para.Add("TeamID", dt.Rows[0]["Team"].ToString());
        }
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = ApsST.GetApsShiftAndTeamRelation(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 更新组 绑定的班次信息;人员信息
    /// </summary>
    protected void BindTeamAndWorkshiftAndEmployee(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        DataTable employees = JsonConvert.DeserializeObject<DataTable>(context.Request["employees"].ToString());
        var para=new Dictionary<string,string>();
        para.Add("TeamID", dt.Rows[0]["TeamID"].ToString());
        para.Add("WorkshiftID", dt.Rows[0]["WorkshiftID"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.Result=ApsST.BindTeamAndWorkshift(para)>0;
        if (result.Result)
        {
            result.Result = ApsST.BindTeamEmployee(para, employees);
        }
        result.Message = result.Result ? "保存成功" : "保存失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取--班组关联的人员信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetTeamEmployees(HttpContext context) {
        var para = new Dictionary<string, string>();
        para.Add("TeamID", context.Request["TeamID"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = ApsST.GetTeamEmployees(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
}